//
//  JDActionSheet.swift
//  funcode
//
//  Created by Wan Jiading on 2017/2/22.
//  Copyright © 2017年 WanJiading. All rights reserved.
//

import UIKit

@objc protocol JDActionSheetDelegate: NSObjectProtocol {
	@objc optional func actionSheet(_ actionSheet: JDActionSheet, indexDidClick: Int)
}

class JDActionSheet: UIView, UITableViewDataSource, UITableViewDelegate, JDActionSheetDelegate {
	
	lazy var blurView: UIVisualEffectView = {
		let blurEffect = UIBlurEffect(style: .light) //创建一个模糊效果
		let blurView = UIVisualEffectView(effect: blurEffect) //创建一个承载模糊效果的视图
		blurView.isUserInteractionEnabled = true
		blurView.frame = MainScreen.bounds
		blurView.alpha = 1
		return blurView
	}()

	var blurBgImageView: UIImageView?

	
	// UI
	lazy var backgroundView: UIView = UIView()
	
	var titleLabel: UILabel = UILabel()
	var contentView: UIView = UIView()
	var cancelButton: UIButton = UIButton()
	
	var maskButton: UIButton = UIButton()
	
	let ScreenWidth = (MainScreen.bounds.size.width)
	let ScreenHeight = (MainScreen.bounds.size.height)
	
	// Data
	var cellID = "ContentCellID"
	var tableView: UITableView?
	
	var dataList: [Book]?
	var delegate: JDActionSheetDelegate?
	var currentSelectedIndex: Int = -1
	
	
	convenience init(title: String?, cancelTitle: String, cycleName: String, dataList bookList: [Book], delegate: JDActionSheetDelegate?) {
		self.init()
		
		/** 设置子视图 begin */
		let titleLabelHeight: CGFloat = 48
		let cancelButtonHeight: CGFloat = 54
		let lineHeight = 1.0 / MainScreen.scale
		let contentViewHeight:CGFloat = 327
		let leftMargin: CGFloat = 15
		
		var titleBottomLine: UIView? = nil
		
		// 标题
		if title != nil && title!.count > 0 {
			let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: titleLabelHeight))
			titleLabel.textAlignment = NSTextAlignment.center
			titleLabel.backgroundColor = UIColor.white
			titleLabel.textColor = UIColor.rgb("333333", alpha: 1)
			titleLabel.font = UIFont.systemFont(ofSize: 16)
			titleLabel.text = title
			backgroundView.addSubview(titleLabel)
			self.titleLabel = titleLabel
			
			// 分割线
			titleBottomLine = UIView(frame: CGRect(x: 0, y: titleLabelHeight, width: ScreenWidth, height: lineHeight))
			titleBottomLine!.backgroundColor = UIColor.rgb("CCCCCC", alpha: 1)
			backgroundView.addSubview(titleBottomLine!)
			
			/// 循环模式
			let cycleLabel = UILabel()
			cycleLabel.textAlignment = NSTextAlignment.left
			cycleLabel.backgroundColor = UIColor.clear
			cycleLabel.textColor = UIColor.rgb("666666", alpha: 1)
			cycleLabel.font = UIFont.systemFont(ofSize: 14)
			cycleLabel.text = cycleName
			cycleLabel.sizeToFit()
			cycleLabel.frame = CGRect(x: leftMargin, y: 0, width: cycleLabel.bounds.size.width, height: titleLabelHeight)
			backgroundView.addSubview(cycleLabel)
			
		}
		
		// 内容
		var contentViewY: CGFloat = 0
		if titleBottomLine != nil {
			contentViewY = titleBottomLine!.frame.maxY
		}
		let contentView = UIView(frame: CGRect(x: 0, y: contentViewY, width: ScreenWidth, height: contentViewHeight))
		contentView.backgroundColor = UIColor.rgb("EFEFEF", alpha: 1)
		backgroundView.addSubview(contentView)
		self.contentView = contentView
		
		// 设置内容
		self.setupTableView(bookList, self.contentView)
		
		// 分割线
		let contentBottomLine = UIView(frame: CGRect(x: 0, y: contentView.frame.maxY, width: ScreenWidth, height: lineHeight))
		contentBottomLine.backgroundColor = UIColor.rgb("CCCCCC", alpha: 1)
		backgroundView.addSubview(contentBottomLine)
		// 取消按钮
		let cancelButton = UIButton(type: UIButtonType.custom)
		cancelButton.frame = CGRect(x: 0, y: contentBottomLine.frame.maxY, width: ScreenWidth, height: cancelButtonHeight)
		cancelButton.backgroundColor = UIColor.white
		cancelButton.titleLabel!.font = UIFont.systemFont(ofSize: 16)
		cancelButton.setTitleColor( UIColor.rgb("333333", alpha: 1), for: [])
		backgroundView.addSubview(cancelButton)
		self.cancelButton = cancelButton
		self.cancelButton.setTitle(cancelTitle, for: [])
		cancelButton.addTarget(self, action: #selector(dismissSelf), for: UIControlEvents.touchUpInside)
		/** 设置子视图 end */
		
		// 添加到 maskButton上
		let maskButton: UIButton = UIButton(type: UIButtonType.custom)
		maskButton.backgroundColor = UIColor.rgb("000000", alpha: 0)
		maskButton.frame = MainScreen.bounds
		maskButton.addTarget(self, action: #selector(dismissSelf), for: UIControlEvents.touchUpInside)
		self.maskButton = maskButton
		
		
		// 层级调整
		
		backgroundView.alpha = 0.8
		
		self.addSubview(maskButton)
		
//		let imgView = UIImageView(image: UIImage(named: "1px"))
//		imgView.contentMode = .scaleToFill
//		self.addSubview(imgView)
//		self.blurBgImageView = imgView
		
		self.addSubview(blurView)
		self.addSubview(backgroundView)
		
		self.frame = maskButton.bounds
		self.isUserInteractionEnabled = true
		
		UIApplication.shared.keyWindow!.addSubview(self)
	}
	
	
	
	func show() {
		let backgroundViewHeight: CGFloat = self.cancelButton.frame.maxY
		self.backgroundView.frame = CGRect(x: 0, y: ScreenHeight, width: ScreenWidth, height: self.backgroundView.frame.size.height)
		self.blurView.frame = CGRect(x: 0, y: ScreenHeight, width: ScreenWidth, height: self.backgroundView.frame.size.height)
		self.blurBgImageView?.frame = CGRect(x: 0, y: ScreenHeight, width: ScreenWidth, height: self.backgroundView.frame.size.height)
//		self.maskButton.frame = CGRect(x: 0, y: 0, width: self.ScreenWidth, height: self.ScreenHeight - backgroundViewHeight)
		
		UIView.animate(withDuration: 0.25, delay: 0, options: UIViewAnimationOptions.curveEaseIn, animations: { [unowned self] in
			self.blurView.frame = CGRect(x: 0, y: self.ScreenHeight - backgroundViewHeight, width: self.ScreenWidth, height: backgroundViewHeight)
			self.backgroundView.frame = CGRect(x: 0, y: self.ScreenHeight - backgroundViewHeight, width: self.ScreenWidth, height: backgroundViewHeight)
			self.blurBgImageView?.frame = CGRect(x: 0, y: self.ScreenHeight - backgroundViewHeight, width: self.ScreenWidth, height: backgroundViewHeight)
//			self.maskButton.frame = CGRect(x: 0, y: 0, width: self.ScreenWidth, height: self.ScreenHeight - backgroundViewHeight)
			self.maskButton.backgroundColor = UIColor.rgb("000000", alpha: 0.3)
			
		}) { (_) in}
	}
	
	func dismissSelf() {
		UIView.animate(withDuration: 0.25, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: { [unowned self] in
			
			self.blurView.frame = CGRect(x: 0, y: self.ScreenHeight, width: self.ScreenWidth, height: self.backgroundView.frame.size.height)
			self.backgroundView.frame = CGRect(x: 0, y: self.ScreenHeight, width: self.ScreenWidth, height: self.backgroundView.frame.size.height)
			self.maskButton.backgroundColor = UIColor.rgb("000000", alpha: 0)
			
			}, completion: {[weak self](finished: Bool) -> Void in
				
				self?.blurView.removeFromSuperview()
				self?.maskButton.removeFromSuperview()
				self?.removeFromSuperview()
		})
	}
	
	deinit {
		DebugLog("\(self)被释放了")
	}
}
